#include "TestWeldJoint.h"

CCScene* TestWeldJoint::scene()
{
	CCScene* sc = CCScene::create();
	TestWeldJoint* layer = TestWeldJoint::create();
	sc->addChild(layer);

	return sc;
}

bool TestWeldJoint::init()
{
	BaseLayer::init();

	initJoint();

	scheduleUpdate();

	return true;
}

void TestWeldJoint::initJoint()
{
	b2Body* bodyA = NULL;
	{
		b2Vec2 vertices[] = { b2Vec2(0.0f, 0.0f), b2Vec2(2.0f, 0.0f), b2Vec2(0.0f, 2.0f) };
		int32 verCount = 3;
		b2PolygonShape shape;
		shape.Set(vertices, verCount);

		b2FixtureDef fixDef;
		fixDef.density = 1.0f;
		fixDef.friction = 0.3f;
		fixDef.restitution = 1.0f;
		fixDef.shape = &shape;

		b2BodyDef bodyDef;
		bodyDef.position.Set(winSize.width/2/PTM_RATIO, winSize.height/2/PTM_RATIO);
		bodyDef.type = b2_dynamicBody;

		bodyA = m_world->CreateBody(&bodyDef);
		bodyA->CreateFixture(&fixDef);
	}

	b2Body* bodyB = NULL;
	{
		b2PolygonShape shape;
		shape.SetAsBox(1, 1);

		b2FixtureDef fixDef;
		fixDef.density = 1.0f;
		fixDef.friction = 0.3f;
		fixDef.restitution = 2.0f;
		fixDef.shape = &shape;

		b2BodyDef bodyDef;
		bodyDef.position.Set(winSize.width/2/PTM_RATIO + 3, winSize.height/2/PTM_RATIO + 2);
		bodyDef.type = b2_dynamicBody;

		bodyB = m_world->CreateBody(&bodyDef);
		bodyB->CreateFixture(&fixDef);
	}

	b2WeldJointDef weldJointDef;
	weldJointDef.Initialize(bodyA, bodyB, bodyA->GetWorldCenter());
	m_world->CreateJoint(&weldJointDef);
}

void TestWeldJoint::update(float delta)
{
	m_world->Step(delta, 8, 1);
}